package com.xgc.book.bookserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgc.book.bookserver.bean.BookCatalogBean;
import com.xgc.book.bookserver.mapper.BookCatalogMapper;
import com.xgc.book.bookserver.service.BookCatalogService;
import com.xgc.book.bookserver.util.ResultJson;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 图书章节表 服务实现类
 * </p>
 *
 * @author wrz
 * @since 2023-11-20
 */
@Service
@Transactional
public class BookCatalogServiceImpl extends ServiceImpl<BookCatalogMapper, BookCatalogBean> implements BookCatalogService {

    @Override
    public ResultJson getPage(int page, int limit, BookCatalogBean bookCatalogBean) {
        Date[] applyTimeRange = bookCatalogBean.getApplyTimeRange();
        Date startTime = null;
        Date endTime = null;
        if (applyTimeRange != null && applyTimeRange.length > 0) {
            startTime = applyTimeRange[0];
            endTime = applyTimeRange[1];
        }

        IPage<BookCatalogBean> iPage = this.page(new Page<>(page, limit),
                new QueryWrapper<BookCatalogBean>()
                        .eq("book_id", bookCatalogBean.getBookId())
                        .like(StringUtils.isNotEmpty(bookCatalogBean.getCatalogName()), "catalog_name",
                                bookCatalogBean.getCatalogName())
                        .eq(bookCatalogBean.getCatalogStatus() != null, "catalog_status",
                                bookCatalogBean.getCatalogStatus())
                        .eq(bookCatalogBean.getStatus() != null, "status", bookCatalogBean.getStatus())
                        .between(applyTimeRange != null && applyTimeRange.length == 2, "apply_time", startTime, endTime)
                        .orderByDesc("order_no")
        );
        return ResultJson.ok(iPage);
    }

    @Override
    public ResultJson getCatalogById(int id) {
        BookCatalogBean bookCatalog = this.getById(id);
        if (bookCatalog == null) {
            return ResultJson.error("编号异常!");
        }
        List<BookCatalogBean> list = this.list(new QueryWrapper<BookCatalogBean>()
                .eq("book_id", bookCatalog.getBook().getBookId()).orderByAsc("order_no"));
        int catalogIndex = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOrderNo().equals(bookCatalog.getOrderNo())) {
                catalogIndex = i;
                break;
            }
        }
        if (catalogIndex == -1) {
            return ResultJson.error("目录集合异常！");
        }

        Map<String, Object> map = new HashMap<>();
        map.put("catalog", bookCatalog);
        if (catalogIndex == 0) {
            map.put("hasNext", true);
            map.put("hasPrev", false);
            map.put("nextId", list.get(catalogIndex + 1).getId());
        } else if (catalogIndex == list.size() - 1) {
            map.put("hasPrev", true);
            map.put("hasNext", false);
            map.put("prevId", list.get(catalogIndex - 1).getId());
        } else {
            map.put("hasNext", true);
            map.put("hasPrev", true);
            map.put("nextId", list.get(catalogIndex + 1).getId());
            map.put("prevId", list.get(catalogIndex - 1).getId());
        }
        return ResultJson.ok(map);
    }
}
